package com.ruoyi.pay.utils;


import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.AlipayConfig;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradePagePayModel;
import com.alipay.api.domain.AlipayTradeQueryModel;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.request.AlipayTradeQueryRequest;
import com.alipay.api.response.AlipayTradePagePayResponse;
import com.alipay.api.response.AlipayTradeQueryResponse;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component
public class ZhifubaoUtill {

    String privateKey = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCoONn7XWTmBsReHEyNR7t35Aw0AqtrMfknMZowJXrTgOMcGnwROnj3cpNnaTunLQVecYMLgmRIugCUzHE+a/O1j20LkGS4iImKSFOevPFzV1ezjaPTfUPLm6GGQ7NEz0+AtMpMWaR5ha91j3T/R6BAhESgsX6E7WJ/6hhfXmqnwo7M2TJpLkilmE+1fp9Cj8YvN9QLXHvW75yIlaDFc2hnpHXFTQmjBTXaR+ZOU0DN0RjudlWMXNlt82RKbPtc9OPKeclt8d8X3kYONY6I8fmalmPNrDAyn4LAZLO2OA4EIbPhcCeVSQF4S6HYqPucjfjRJCkb9F00UNiG5c3ovgf/AgMBAAECggEAcsH10oiv4EwVga4EewzIUs17la+MiSAaPCaWbOnklIyEYhOfN5GQHB5JeG5Nh4oq83a0UxGsoABjjR660j6Kjv09AnqKe2iJi5Azm0KB5JFc/txNOmVl7mxJEHL2xKY4MjEjIFq23T/ZpnUq90tBg0G4H3d2O/BFFcX4wnaCg9YTK8EhpcIVPPdCLR+lgHGjEU0foG4kWX+V6NtMvc1j5cn9EbGRl6MzoUMeKirPcrqWhDPS7k9sJkouPhEuGZAB/yeH5k/ewrPPUosh175XozZ/dbab0zuu4Q6szjVUOHAagvLGGGczqfunqe0lOSIddnJECienU2wh1ZqBzjGM4QKBgQDzG7Gd5BUXeXy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GlzHR1dgHTUiHvBwDcOTpKHZFYZfdOtbrdCs/+8mceVC9NJ+bKYbWuZa8Exj1OoWK8OAd1QBKeJ7i8t4BDG0eLmXJWNpKujA4T1r57bWK4jWo81yorfMvQ4kN6PGJ7pZmuETXevr1LgpPW7m7f/RvSa0XvPD0ogGQSbHBfJOFNftNIvjwIDAQAB";
    //生成支付订单接口
    public Map pay(String orderId, String price) throws AlipayApiException {
        AlipayConfig alipayConfig = new AlipayConfig();
        alipayConfig.setServerUrl("https://openapi-sandbox.dl.alipaydev.com/gateway.do");
        alipayConfig.setAppId("9021000141612420");
        alipayConfig.setPrivateKey(privateKey);
        alipayConfig.setFormat("json");
        alipayConfig.setAlipayPublicKey(alipayPublicKey);
        alipayConfig.setCharset("UTF8");
        alipayConfig.setSignType("RSA2");
        AlipayClient alipayClient = new DefaultAlipayClient(alipayConfig);
        AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();
        //同步回调
        request.setReturnUrl("http://localhost:9302/alipayExam/result");
//        request.setReturnUrl("");

        AlipayTradePagePayModel model = new AlipayTradePagePayModel();
        model.setOutTradeNo(orderId);
        model.setTotalAmount(price);
        model.setSubject("拼夕夕商城订单");
        model.setProductCode("FAST_INSTANT_TRADE_PAY");
        request.setBizModel(model);
        AlipayTradePagePayResponse response = alipayClient.pageExecute(request);
//        return response.getBody();
        System.out.println(response.getBody());
        HashMap<Object, Object> map = new HashMap<>();
        if (response.isSuccess()) {
            System.out.println("调用成功");
            map.put("code",200);
            map.put("form",response.getBody());
        } else {
            map.put("code",400);
        }
        return map;
    }


    //查询接口
    public Boolean queryResult(String tradeNo) throws AlipayApiException {
        AlipayConfig alipayConfig = new AlipayConfig();
        alipayConfig.setServerUrl("https://openapi-sandbox.dl.alipaydev.com/gateway.do");
        alipayConfig.setAppId("9021000141612420");
        alipayConfig.setPrivateKey(privateKey);
        alipayConfig.setFormat("json");
        alipayConfig.setAlipayPublicKey(alipayPublicKey);
        alipayConfig.setCharset("UTF8");
        alipayConfig.setSignType("RSA2");
        AlipayClient alipayClient = new DefaultAlipayClient(alipayConfig);
        AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
        AlipayTradeQueryModel model = new AlipayTradeQueryModel();
        model.setOutTradeNo(tradeNo);
        request.setBizModel(model);
        AlipayTradeQueryResponse response = alipayClient.execute(request);
        System.out.println(response.getBody());
        return response.isSuccess();
    }
}
